package com.spider.silence.worker;

import com.spider.silence.abstarct.downloader.AbstractCommonDownLoader;
import com.spider.silence.proxy.ProxyPool;
import com.spider.silence.task.XiQinHuiGuanModuleTask;
import com.spider.silence.task.XiQinHuiGuanTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.PropertiesUtil;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.Page;

import java.net.URLDecoder;

/**
 * @author liyuanguo
 * @version V1.0
 * @time 2017/3/28 11:48
 * @description
 */
public class XiQinHuiGuanModuleWorker extends AbstractCommonDownLoader implements Runnable{
    private Logger logger = Logger.getLogger(MalaModuleWorker.class);
    @Override
    public void run() {

        while(true&&!Thread.interrupted()){
            try {
                XiQinHuiGuanModuleTask moduleTask = (XiQinHuiGuanModuleTask) CommonTaskManager.take("xiQinHuiGuanModule");
                if(null!=moduleTask){
                    String moduleUrl = moduleTask.getModuleUrl();
                    long startTime = System.currentTimeMillis();
                    long nowTime = startTime;
                    while (nowTime - startTime < Long.valueOf(PropertiesUtil.getVal("XiQinHuiGuan.TimeOut"))) {
                        try {
                            Document document = get(moduleUrl, true);
                            if(document != null) {
                                //获取每个模块下的第一页所有帖子的url、组装任务
                                Elements elements = document.select("table[id=\"threadlisttableid\"] tbody tr th[class=\"common\"] a[class=\"s xst\"]");
                                for (int j = 0; j < elements.size(); j++) {
                                    String forumUrl = elements.get(j).attr("href");
                                    System.out.println("forumUrl:"+forumUrl);
                                    XiQinHuiGuanTask task = new XiQinHuiGuanTask(URLDecoder.decode(forumUrl, "UTF-8"));
                                    CommonTaskManager.add("xiQinHuiGuan", task);
                                }
                                break;
                            }
                        } catch (Exception e1) {
                            System.err.println("获取版块任务异常, 重新尝试。。");
                            Thread.sleep(3000);
                            logger.error(e1.getMessage(), e1);
                        }finally{
                            nowTime = System.currentTimeMillis();
                        }
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
                continue;
            }
        }
    }

}
